﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Face_Recognition
{
    public partial class MainWindow : Form
    {
        ImageIO imageIO = new ImageIO();
        ImageIO imageSource = new ImageIO();
        Filters filters ;
        Graphics G;
        Haar.Detector detector;
        IntegralImage integralImage;
        public MainWindow()
        {
            InitializeComponent();
           
            filters = new Filters(imageIO.Bitmap);

            pictureBox1.Image = filters.bitmap;

//            G = Graphics.FromImage(pictureBox1.Image);
//            G.DrawRectangle(new Pen(new SolidBrush(Color.Red), 2), filters.FindFace());
        }

        private void loadImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            loadImageDialog.ShowDialog();
        }

        private void Launch_Click(object sender, EventArgs e)
        {
            this.UseWaitCursor = true;
            drawBounds(filters.FindFacesViaHoles());
            this.UseWaitCursor = false;
        }

        private void loadImageDialog_FileOk(object sender, CancelEventArgs e)
        {
            imageIO.setImage(loadImageDialog.FileName);
            filters = new Filters(imageIO.Bitmap);
            pictureBox1.Image = filters.bitmap;
        }

        private void drawBounds(List<Rectangle> rectangles)
        {
            Graphics graphics = Graphics.FromImage(pictureBox1.Image);
            //graphics.Clear(Color.Transparent);
            //pictureBox1.Invalidate();
            foreach (Rectangle r in rectangles)
            {
                graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red), 1), r);
            }

            if (rectangles.Count > 0)
                foundLabel.Text = "Totally found sir!";

            pictureBox1.Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            detector = new Haar.Detector(new Haar.FaceHaarCascade(),15,1.5f);
            integralImage = new IntegralImage((Bitmap)pictureBox1.Image);
            Rectangle[] rects = detector.ProcessFrame(integralImage);
            Graphics graphics = Graphics.FromImage(pictureBox1.Image);

            foreach(Rectangle r in rects)
            {
                graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red), 2), r);
            }
            pictureBox1.Invalidate();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void loadDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = TextureFilters.GaussianBlur((Bitmap)pictureBox1.Image);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.UseWaitCursor = true;
            drawBounds(filters.FindFacesViaRegions());
            this.UseWaitCursor = false;
        }

        private void skinMapCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (skinMapCheck.Checked)
                Settings.ShowSkinMap = true;
            else
                Settings.ShowSkinMap = false;
        }

        private void rawEdgesCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (rawEdgesCheck.Checked)
                Settings.ShowEdges = true;
            else
                Settings.ShowEdges = false;
        }

        private void thinnedEdgesCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (thinnedEdgesCheck.Checked)
                Settings.ShowThinnedEdges = true;
            else
                Settings.ShowThinnedEdges = false;
        }

        private void textureCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (textureCheck.Checked)
                Settings.TextureCheck = true;
            else
                Settings.TextureCheck = false;
        }

        private void sobelBoundsCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (sobelBoundsCheck.Checked)
                Settings.SobelBoundaries = true;
            else
                Settings.SobelBoundaries = false;
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
                Settings.ColorSobelBoundaries = true;
            else
                Settings.ColorSobelBoundaries = false;
        }

        private void scaleUpDown_ValueChanged(object sender, EventArgs e)
        {
            Settings.Scaling = Convert.ToDouble(scaleUpDown.Value);
        }

    }
}
